<title>权限管理</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a><cite>系统管理</cite></a>
        <a><cite>权限管理</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-permission-formlist">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="name" placeholder="权限名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="code" placeholder="权限编号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <select name="moduleId" id="topMenu">
                            <option value="">请选择</option>
                            <option value="0">无模块</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-permission" lay-submit
                            lay-filter="LAY-sys-permission-search">
                        搜索
                    </button>
                    <button class="layui-btn layuiadmin-btn-permission" data-type="add">
                        添加
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div class="LAY-permission-module-btns" style="margin-bottom: 10px;">
                <button class="layui-btn layui-btn-primary layui-btn-sm" data-events="module">批量修改模块</button>
            </div>
            <table id="LAY-sys-permission-table" lay-filter="LAY-sys-permission-table"></table>
            <script type="text/html" id="table-permission-admin">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">
                    <i class="layui-icon layui-icon-edit"></i>编辑</a>
                <!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i-->
                <!--class="layui-icon layui-icon-delete"></i>删除</a>-->
            </script>
        </div>
    </div>
</div>


<script type="text/html" id="modelPermission">
    <form id="modelPermissionForm" lay-filter="modelPermissionForm" class="layui-form model-form">

        <div class="layui-form-item">
            <label class="layui-form-label">权限模块</label>
            <div class="layui-input-block">
                <select name="moduleId" id="selectModule">
                    <option value="">请选择</option>
                    <option value="0">无模块</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">权限名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入权限名称" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">权限编号</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入权限编号" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">权限路径</label>
            <div class="layui-input-block">
                <input name="action" placeholder="请输入权限路径" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">权限描述</label>
            <div class="layui-input-block">
                <input name="desc" placeholder="请输入权限描述" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-fluid" lay-filter="modelPermissionSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<script type="text/html" id="modelChangeModule">
    <form id="modelChangeModuleForm" lay-filter="modelChangeModuleForm" class="layui-form model-form">
        <div class="layui-form-item">
            <label class="layui-form-label">权限模块</label>
            <div class="layui-input-block">
                <div id="radioChangeModule"></div>
                <!--                <select name="moduleId" id="selectChangeModule">-->
                <!--                    <option value="">请选择</option>-->
                <!--                    <option value="0">无模块</option>-->
                <!--                </select>-->
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-fluid" lay-filter="modelChangeModuleSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<script>
    layui.use(['admin', 'table', 'setter'], function () {
        var $ = layui.$
            // , setter = layui.setter
            , admin = layui.admin
            , view = layui.view
            , table = layui.table
            , form = layui.form
            , setter = layui.setter;

        var apiUrl = setter.apiUrl;

        admin.req({
            url: apiUrl + '/mapi/permission/pm/drop_list'
            , method: 'get'
            , done: function (response) {
                if (response.code === 0) {
                    var list = response.data;
                    var html = "";
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value=" + list[i]["id"] + ">" + list[i]["name"] + "</option>";
                    }
                    $("#topMenu").append(html);
                    form.render('select');
                }
            }
        });


        //管理员管理
        table.render({
            elem: '#LAY-sys-permission-table'
            , url: apiUrl + '/mapi/permission/p/list'
            , method: 'post'
            , page: true
            , contentType: 'application/json'
            , headers: {'token': layui.data(setter.tableName).token}
            , request: {
                pageName: 'pageNo'
                , limitName: 'pageSize'
            }
            , response: {
                statusCode: 0, //规定数据状态的字段名称，默认：code
            }
            , parseData: function (res) {
                if (res.code === 0) {
                    //将原始数据解析成 table 组件所规定的数据
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.list //解析数据列表
                    };
                } else {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                    };
                }
            }
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', width: 60, title: 'id'}
                , {field: 'name', title: '权限名称'}
                , {field: 'code', title: '权限编号'}
                , {field: 'action', title: '权限路径'}
                , {field: 'moduleName', width: 100, title: '模块', templet: '<div>{{d.module?d.module.name:""}}</div>'}
                , {field: 'desc', title: '权限描述'}
                , {title: '操作', fixed: 'right', toolbar: '#table-permission-admin'}
            ]]
            , text: {
                none: '未检索到数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            }
        });


        //监听搜索
        form.on('submit(LAY-sys-permission-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-sys-permission-table', {
                where: field
            });
        });

        // 工具条点击事件
        table.on('tool(LAY-sys-permission-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'permission') { // 添加
                admin.popup({
                    title: '权限赋权'
                    , id: 'LAY-popup-permission-permission'
                    , area: ['50%', '80%']
                    , success: function (layero, index) {
                        view(this.id).render('sys/permissionpermission').done(function () {
                            //监听提交
                            form.on('submit(LAY-sys-permission-permission-submit)', function (data) {
                                layer.msg("提交啦");
                            });
                        });
                    }
                });
            }
        });

        function showEditModel(curData) {
            admin.popup({
                title: '编辑权限'
                // , area: ['420px', '430px']
                , id: 'LAY-popup-permission-add'
                , content: $('#modelPermission').html()
                , success: function (layero, index) {
                    admin.req({
                        url: apiUrl + '/mapi/permission/pm/drop_list'
                        , method: 'get'
                        , done: function (response) {
                            if (response.code === 0) {
                                var list = response.data;
                                var html = "";
                                for (var i = 0; i < list.length; i++) {
                                    html += "<option value=" + list[i]["id"] + ">" + list[i]["name"] + "</option>";
                                }
                                $("#selectModule").append(html);
                                var moduleId = curData ? curData.moduleId : 0;
                                $("#selectModule").val(moduleId);
                                form.render('select');
                            }
                        }
                    });
                    form.val('modelPermissionForm', curData);  // 回显数据
                    // 表单提交事件
                    form.on('submit(modelPermissionSubmit)', function (data) {
                        //提交 Ajax 成功后，关闭当前弹层并重载表格
                        admin.req({
                            url: apiUrl + '/mapi/permission/p/update/' + curData.id
                            , method: 'put'
                            , data: JSON.stringify(data.field)
                            , done: function (response) {
                                if (response.code === 0) {
                                    layer.msg('保存成功', {
                                        offset: '15px', icon: 1, time: 1000
                                    }, function () {
                                        layer.close(index); //执行关闭
                                        layui.table.reload('LAY-sys-permission-table'); //重载表格
                                    });
                                } else {
                                    layer.msg(response.msg, {
                                        offset: '15px', icon: 2
                                    });
                                }
                            }
                        });
                        return false;
                    });
                }
            });
        }

        //事件
        var active = {
            add: function () {
                var checkStatus = table.checkStatus('LAY-sys-permission-table'), curData = checkStatus.data; //得到选中的数据
                admin.popup({
                    title: '添加权限'
                    // , area: ['420px', '430px']
                    , id: 'LAY-popup-permission-add'
                    , content: $('#modelPermission').html()
                    , success: function (layero, index) {
                        admin.req({
                            url: apiUrl + '/mapi/permission/pm/drop_list'
                            , method: 'get'
                            , done: function (response) {
                                if (response.code === 0) {
                                    var list = response.data;
                                    var html = "";
                                    for (var i = 0; i < list.length; i++) {
                                        html += "<option value=" + list[i]["id"] + ">" + list[i]["name"] + "</option>";
                                    }
                                    $("#selectModule").append(html);
                                    form.render('select');
                                }
                            }
                        });
                        // 表单提交事件
                        form.on('submit(modelPermissionSubmit)', function (data) {
                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                            admin.req({
                                url: apiUrl + '/mapi/permission/p/add'
                                , method: 'post'
                                , data: JSON.stringify(data.field)
                                , done: function (response) {
                                    if (response.code === 0) {
                                        layer.msg('保存成功', {
                                            offset: '15px', icon: 1, time: 1000
                                        }, function () {
                                            layer.close(index); //执行关闭
                                            layui.table.reload('LAY-sys-permission-table'); //重载表格
                                        });
                                    } else {
                                        layer.msg(response.msg, {
                                            offset: '15px', icon: 2
                                        });
                                    }
                                }
                            });
                            return false;
                        });
                    }
                });
            }
        };


        var events = {
            module: function () {
                var checkStatus = table.checkStatus('LAY-sys-permission-table')
                    , data = checkStatus.data; //获得选中的数据
                if (data.length === 0) return layer.msg('请先选择权限');
                var ids = [];
                for (var i = 0; i < data.length; i++) {
                    ids.push(data[i].id);
                }
                admin.popup({
                    title: '批量修改模块'
                    , area: ['420px', '380px']
                    , id: 'LAY-popup-permission-change-module'
                    , content: $('#modelChangeModule').html()
                    , success: function (layero, index) {
                        admin.req({
                            url: apiUrl + '/mapi/permission/pm/drop_list'
                            , method: 'get'
                            , done: function (response) {
                                if (response.code === 0) {
                                    var list = response.data;
                                    var html = '<input type="radio" name="moduleId" value="0" title="无模块" checked>';
                                    for (var i = 0; i < list.length; i++) {
                                        html += '<input type="radio" name="moduleId" value="' + list[i]["id"] + '" title="' + list[i]["name"] + '" >';
                                    }
                                    $("#radioChangeModule").append(html);
                                    form.render();
                                }
                            }
                        });
                        var upIds = ids.join(',');
                        // 表单提交事件
                        form.on('submit(modelChangeModuleSubmit)', function (data) {
                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                            admin.req({
                                url: apiUrl + '/mapi/permission/p/update/' + upIds + '/' + data.field.moduleId
                                , method: 'put'
                                , data: JSON.stringify(data.field)
                                , done: function (response) {
                                    if (response.code === 0) {
                                        layer.msg('保存成功', {
                                            offset: '15px', icon: 1, time: 1000
                                        }, function () {
                                            layer.close(index); //执行关闭
                                            layui.table.reload('LAY-sys-permission-table'); //重载表格
                                        });
                                    } else {
                                        layer.msg(response.msg, {
                                            offset: '15px', icon: 2
                                        });
                                    }
                                }
                            });
                            return false;
                        });
                    }
                });

            }
        };


        $('.layui-btn.layuiadmin-btn-permission').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        $('.LAY-permission-module-btns .layui-btn').on('click', function () {
            var thisEvent = $(this).data('events');
            events[thisEvent] && events[thisEvent].call(this);
        });

    });
</script>